package com.labmi.experiment;

import javax.annotation.Generated;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;

@Entity
@IdClass(ItemStockID.class) //Composite Primary key reference..
public class ItemStock {

	@Id
	private String itemCode;
	
	@ManyToOne(cascade={ CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE } )
	private Item item; 
	
	@Id
	@ManyToOne(cascade={ CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE } )
	@JoinColumn(name="SIZE_SIZENAME")
	private Size size;
	
	@Id
	@ManyToOne(cascade={ CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE } )
	@JoinColumn(name="COLOR_HEXCODE")
	private Color color;
	


	//Composite key needs to replicate @Embeddable (see ItemStockID.class) attributes....
	private int availableQuantity;
	
	
	private int selectedQuantity;
	
	
	
	public ItemStock() {
	}
	
	public ItemStock(String itemCode, Size size, Color color, int quantity) {
		this.itemCode = itemCode;
		this.setColor(color);
		this.setSize(size);
		this.availableQuantity = quantity;
	}

	public void setItem(Item item) {
		this.item = item;
		synchronized (this) {
			if( ! item.getItemStockList().contains(this))
				item.addItemStock(this); //Bidirectional relationship
		}
		
	}

	public Item getItem() {
		return item;
	}

	public static ItemStock createItemStockInfo(String itemCode, Size size, Color color, int quantity)
	{
		ItemStock anItemStock = new ItemStock(itemCode, size, color, quantity);
		
		return anItemStock; 
		
	}

	public void setAvailableQuantity(int quantity) {
		this.availableQuantity = quantity;
	}

	public int getAvailableQuantity() {
		return availableQuantity;
	}

	
	public void setColor(Color color) {
		this.color = color;
		synchronized (this) {
		if( ! color.getItemStockList().contains(this))
			color.addItemStock(this); //Bidirectional relationship
		}
	}

	public Color getColor() {
		return color;
	}
	
	
	
	public void setSize(Size size) {
		this.size = size;
		if( ! size.getItemStockList().contains(this))
			size.addItemStock(this); //Bidirectional relationship
		
	}

	public Size getSize() {
		return size;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getItemCode() {
		return itemCode;
	}

	@Override
	public String toString() {
		return  "Item Code: " + itemCode + " Size: " +  size.getSizeName() + " Color: " + color + " Quantity: " + availableQuantity;
	}

	public void setSelectedQuantity(int selectedQuantity) {
		this.selectedQuantity = selectedQuantity;
	}

	public int getSelectedQuantity() {
		return selectedQuantity;
	}
	
}
